<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form
                class="mb-[-16px]"
                :model="queryParams"
                inline
            >
                <el-form-item label="文章标题" prop="title">
                    <el-input class="w-[280px]" v-model="queryParams.title" clearable placeholder="请输入文章标题" />
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select v-model="queryParams.status" placeholder="请选择状态" clearable class="w-[150px]">
                        <el-option label="全部" value="" />
                        <el-option label="正常" :value="1" />
                        <el-option label="禁用" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增文章
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="文章标题" prop="title" show-overflow-tooltip min-width="200"/>
                    <el-table-column label="作者" prop="author" show-overflow-tooltip min-width="120"/>
                    <el-table-column label="摘要" prop="summary" show-overflow-tooltip min-width="250"/>
                    <el-table-column label="封面图" prop="cover" show-overflow-tooltip min-width="100">
                        <template #default="{ row }">
                            <el-image v-if="row.cover" :src="row.cover" style="width: 60px; height: 40px;" fit="cover"/>
                            <span v-else class="text-gray-400">无封面</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="状态" prop="status" show-overflow-tooltip min-width="80">
                        <template #default="{ row }">
                            <el-tag :type="row.status == 1 ? 'success' : 'danger'">
                                {{ row.status == 1 ? '正常' : '禁用' }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="创建时间" prop="create_time" show-overflow-tooltip min-width="160">
                        <template #default="{ row }">
                            {{ timeFormat(row.create_time, 'yyyy-mm-dd hh:MM:ss') }}
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" width="160" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
                            <el-button v-if="row.status == 1" type="danger" link @click="handleDelete(row.id)">删除</el-button>
                            <el-button v-if="row.status == 0" type="primary" link @click="handleRecover(row.id)">恢复</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="articleManage">
import { usePaging } from '@/hooks/usePaging'
import { apiMuArticleLists, apiMuArticleDelete, apiMuArticleRecover } from '@/api/graveyardManage/article'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import { reactive, ref, shallowRef, nextTick, onMounted } from 'vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    title: '',
    status: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiMuArticleLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiMuArticleDelete({ id })
    getLists()
}

// 恢复
const handleRecover = async (id: number | any[]) => {
    await feedback.confirm('确定要恢复？')
    await apiMuArticleRecover({ id })
    getLists()
}

onMounted(() => {
    getLists()
})
</script>

<style scoped>
</style>